package com.yonyou.pmclouds.stagecheck.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.entity.AdditionalPropertiesVOWrapper;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseInfo;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.inspectitem.rmiitf.InspectItemService;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.plan.entity.ScheduleItemVO;
import com.yonyou.pmclouds.stagecheck.entity.StageCheckVO;
import com.yonyou.pmclouds.stagecheck.rmitf.StageCheckMaintain;
import com.yonyou.pmclouds.stagecheck.rmitf.StageCheckQuery;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workquality.entity.WorkqualityCheckTypeEnum;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author chulf
 * @Date 2020/4/1
 * @Description
 **/
@RestController
@RequestMapping(value = {"/mobile/stageCheck","/stageCheck"})
public class StageCheckMobileController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private StageCheckMaintain stageCheckMaintain;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private StageCheckQuery stageCheckQuery;
    @Autowired
    private UserInfoQuery userQuery;
    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;
    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private InspectItemService inspectItemService;

    private static final String[] USER_ID_FIELDS = {StageCheckVO.CREATOR};
    private static final String[] USER_NAME_FIELDS = {StageCheckVO.CREATOR_NAME};

    @Insert
    @PostMapping("/add")
    @PMApprove
    public StageCheckVO add(@RequestBody @Validated StageCheckVO stageCheckVO)
            throws BusinessException {
        StageCheckVO resultVO = stageCheckMaintain.insert(stageCheckVO);
        userQuery.tranUserInfoField(new StageCheckVO[]{resultVO}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return resultVO;
    }

    @PostMapping("/update")
    @PMApprove(trigger = "commit")
    public StageCheckVO update(@RequestBody @Validated StageCheckVO stageCheckVOVO)
            throws BusinessException {

        StageCheckVO resultVO = stageCheckMaintain.update(stageCheckVOVO);
        userQuery.tranUserInfoField(new StageCheckVO[]{resultVO}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return resultVO;
    }


    /**
     * 通过项目查询所有的阶段验收单信息
     *
     * @param pkProject
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/queryByProject")
    public AdditionalPropertiesVOWrapper[] queryByProject(String pkProject)
            throws BusinessException {
        StageCheckVO[] vos = stageCheckQuery.queryByProject(pkProject);
        if (ArrayUtils.isEmpty(vos)) {
            return new AdditionalPropertiesVOWrapper[0];
        }
        Set<String> constructionUnitPks = new HashSet<>();
        // 取到所有施工单位的pk,获取施工单位信息
        for (StageCheckVO comprehensiveAcceptanceVO : vos) {
            constructionUnitPks.add(comprehensiveAcceptanceVO.getPkConstructionunit());
        }
        Map<String, EnterpriseInfo> enterpriseInfoMap = enterpriseInfoQuery
                .queryEnterpriseByIds(constructionUnitPks.toArray(new String[0]));
        for (StageCheckVO comprehensiveAcceptanceVO : vos) {
            EnterpriseInfo enterpriseInfo = enterpriseInfoMap.get(comprehensiveAcceptanceVO.getPkConstructionunit());
            comprehensiveAcceptanceVO.setConstructionName(enterpriseInfo == null ? "" : enterpriseInfo.getName());
        }
        userQuery.tranUserInfoField(vos, USER_ID_FIELDS, USER_NAME_FIELDS);
        return propertiesProcessor.wrapAdditionalProps(vos);
    }

    /**
     * 查询阶段验收详情
     * @param pkStageCheck
     * @return
     * @throws BusinessException
     */

    @RequestMapping("/getStageCheckDetail")
    public StageCheckVO getStageCheckDetail(String pkStageCheck) throws BusinessException {
        StageCheckVO stageCheckVO = stageCheckQuery.queryOneByPk(pkStageCheck);
        // 填充施工单位信息
        String pkConstructionunit = stageCheckVO.getPkConstructionunit();
        if (!StringUtils.isEmpty(pkConstructionunit)) {
            EnterpriseInfo enterpriseInfo = enterpriseInfoQuery.queryEnterpriseById(pkConstructionunit);
            stageCheckVO.setConstructionName(enterpriseInfo == null ? "" : enterpriseInfo.getName());
        }
        userQuery.tranUserInfoField(new StageCheckVO[]{stageCheckVO}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return stageCheckVO;
    }

    /**
     * 查看验收项详细信息
     *
     * @param pkInspectitem
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/getInspectItemDetail")
    public InspectItemVO queryInspectItemVO(String pkInspectitem)
            throws BusinessException {
        return inspectItemService.queryOne(pkInspectitem);
    }


}
